package com.rogue.employee.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.cglib.CglibUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rogue.common.core.constant.CommonResultCode;
import com.rogue.employee.domain.HsDepartment;
import com.rogue.employee.domain.HsEmployee;
import com.rogue.employee.domain.HsEmployeeDepartment;
import com.rogue.employee.domain.dto.DeptDTO;
import com.rogue.employee.domain.dto.ExamineDTO;
import com.rogue.employee.domain.dto.HsEmployeeDTO;
import com.rogue.employee.domain.vo.EmployeeVo;
import com.rogue.employee.mapper.HsDepartmentMapper;
import com.rogue.employee.mapper.HsEmployeeDepartmentMapper;
import com.rogue.employee.mapper.HsEmployeeMapper;
import com.rogue.employee.service.HsEmployeeDepartmentService;
import com.rogue.employee.service.HsEmployeeService;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
* @author Yeah
* @description 针对表【hs_employee(员工表)】的数据库操作Service实现
* @createDate 2022-03-16 14:33:08
*/
@Service
public class HsEmployeeServiceImpl extends ServiceImpl<HsEmployeeMapper, HsEmployee>
    implements HsEmployeeService{

    @Resource
    private HsEmployeeDepartmentMapper hsEmployeeDepartmentMapper;
    @Resource
    private HsEmployeeDepartmentService hsEmployeeDepartmentService;
    @Resource
    private HsEmployeeMapper hsEmployeeMapper;
    @Resource
    private HsDepartmentMapper hsDepartmentMapper;

    @Override
    public List<HsEmployee> getEmpByDeptId(Long id) {
        // 根据部门 id 查出关联的员工id 提取员工id
        List<Long> empIds = hsEmployeeDepartmentMapper.selectList(
                new QueryWrapper<HsEmployeeDepartment>().eq("dept_id", id)).stream()
            .map(HsEmployeeDepartment::getEmpId).collect(
                Collectors.toList());

        // 获取员工属性
        return this.baseMapper.selectBatchIds(empIds);

    }

    @Override
    public Boolean updateDeptOfEmployee(DeptDTO dto) {

        // 转换为关联对象再进行插入
        HsEmployeeDepartment hsEmployeeDepartment = new HsEmployeeDepartment();
        BeanUtil.copyProperties(dto,hsEmployeeDepartment);
        return hsEmployeeDepartmentMapper.insert(hsEmployeeDepartment) > 0;
    }

    @Override
    public String examineIdCard(ExamineDTO examineDTO) {

        this.update(new UpdateWrapper<HsEmployee>().lambda()
            .eq(HsEmployee::getId,examineDTO.getEmpId())
            .eq(HsEmployee::getRealName,examineDTO.getName())
            .set(StrUtil.isNotBlank(examineDTO.getIdCard()),HsEmployee::getIdCard,examineDTO.getIdCard())
            .set(HsEmployee::getIsLocked,examineDTO.getStatus()));
        // JSONUtil.s TODo 要把字符串转成 json 吗？
        // 审核不通过
        if (CommonResultCode.LOCK.equals(examineDTO.getStatus())){
            return "审核通过";
        }
        return "审核不通过";
    }

    @Override
    public PageInfo<EmployeeVo> listEmployee(HsEmployeeDTO hsEmployeeDTO) {
        PageInfo<EmployeeVo> pageInfo = PageHelper.startPage(hsEmployeeDTO.getPage(),
                hsEmployeeDTO.getCount())
            .doSelectPageInfo(() ->hsEmployeeMapper.selectList(new LambdaQueryWrapper<HsEmployee>()
                .like(StrUtil.isNotBlank(hsEmployeeDTO.getRealName()), HsEmployee::getRealName,
                    hsEmployeeDTO.getRealName())
                .eq(Objects.nonNull(hsEmployeeDTO.getDeptId()), HsEmployee::getDeptId,
                    hsEmployeeDTO.getDeptId())
            ));
        // 调用了 CglibUtil 来进行列表类的转换
        pageInfo.setList(CglibUtil.copyList(pageInfo.getList(), EmployeeVo::new));
        pageInfo.getList().stream().forEach(x ->{
            HsDepartment hsDepartment = hsDepartmentMapper.selectById(x.getDeptId());
            if (Objects.nonNull(hsDepartment)){
                x.setDeptName(hsDepartment.getName());
            }
            else {
                x.setDeptName("未设置");
            }
        });
        return pageInfo;
    }

    @Override
    public Boolean lockOrUnlockEmployee(Long id,Integer status) {
        HsEmployee hsEmployee = new HsEmployee();
        if (Objects.equals(1,status)){
            status = 0;
        }
        else if (Objects.equals(0,status)){
            status = 1;
        }
        hsEmployee.setIsLocked(status);
        hsEmployee.setId(id);
        return hsEmployeeMapper.updateById(hsEmployee) > 0;
    }

    @Override
    public EmployeeVo selectEmpById(Long id) {
        HsEmployee hsEmployee = this.baseMapper.selectById(id);
        return BeanUtil.copyProperties(hsEmployee,EmployeeVo.class);
    }
}




